package com.example.musicplayer;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class logActivity extends AppCompatActivity {
    private static final String TAG="logActivity";
    final String sUser="admin";
    final String sPwd="123456";
    int errorNum=0;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main,menu);
        return super.onCreateOptionsMenu(menu);
    }

    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch(item.getItemId())
        {
            case R.id.itDetail:
                AlertDialog dialog = new AlertDialog.Builder(this)
                        .setIcon(R.drawable.log_1)//设置标题的图片
                        .setTitle("App详情")//设置对话框的标题
                        .setMessage("佳宝自制播放器v1.0\n有问题详情q：724032931")//设置对话框的内容
                        //设置对话框的按钮
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //Toast.makeText(logActivity.this, "点击了取消按钮", Toast.LENGTH_SHORT).show();
                                dialog.dismiss();
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //Toast.makeText(logActivity.this, "点击了确定的按钮", Toast.LENGTH_SHORT).show();
                                dialog.dismiss();
                            }
                        }).create();
                dialog.show();


                break;
            case R.id.itDes:
            {
                finish();
                break;
            }
            default:
            {

            }
        }
        return true;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log);

        final EditText edtUser=(EditText)findViewById(R.id.edtUser);
        final EditText edtPwd=(EditText)findViewById(R.id.edtPwd);

        //按下登录按钮
        Button butLog =(Button) findViewById(R.id.butLog);
        butLog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String User=edtUser.getText().toString();
                String pwd=edtPwd.getText().toString();
                if(User.equals(sUser)&&pwd.equals(sPwd))
                {
                    Intent intent =new Intent(logActivity.this,MainActivity.class);

                    Toast.makeText(logActivity.this, "登录成功！", Toast.LENGTH_SHORT).show();
                    errorNum=0;

                    startActivity(intent);

                }
                else
                {
                    errorNum++;
                    if(errorNum >3)
                    {
                        Toast.makeText(logActivity.this, "错了这么多次，封你几天！", Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        Toast.makeText(logActivity.this, "登陆失败！还有"+(3- errorNum)+"次机会", Toast.LENGTH_SHORT).show();
                    }

                }
            }
        });
    }
}